package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-26
 */
@Api(description = "课程接口")
@RestController
@RequestMapping("/eduService/course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //分页带条件查询课程列表
    //TODO  完善条件查询带分页
    @GetMapping("/courseListByConditionAndPage")
    @ApiOperation(value = "分页带条件查询课程列表")
    public R getCourseListByConditionAndPage(@RequestParam Map<String, Object> params) {
        //每页记录数
        Integer limit = null;
        if (StringUtils.isBlank(String.valueOf(params.get("limit")))){
            limit = 10;
        }else {
            try{
                limit = Integer.valueOf(String.valueOf(params.get("limit")));
            }catch (Exception e) {
                return R.error().message("分页参数（每页记录数）格式有误");
            }
        }

        //页码
        Integer page = null;
        if (StringUtils.isBlank(String.valueOf(params.get("page")))){
            page = 1;
        }else {
            try{
                page = Integer.valueOf(String.valueOf(params.get("page")));
            }catch (Exception e) {
                return R.error().message("分页参数（页码）格式有误");
            }
        }

        //课程名称
        String courseName = null;
        if (StringUtils.isBlank((String)params.get("courseName"))){

        }else {
            courseName = (String)params.get("courseName");
        }

        //发布状态
        String courseStatus = null;
        if (StringUtils.isBlank((String)params.get("courseStatus"))){

        }else {
            courseStatus = (String)params.get("courseStatus");
        }

        //根据条件查询课程总数
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(courseName)){
            queryWrapper.like("title", courseName);
        }
        if (!StringUtils.isBlank(courseStatus)){
            queryWrapper.eq("status", courseStatus);
        }
        int count = courseService.count(queryWrapper);

        //条件查询课程分页数据
        List<EduCourse> list = courseService.getCourseListByConditionAndPage(courseName, courseStatus, limit, page);

        HashMap<String,Object> map = new HashMap<>();

        map.put("count", count);
        map.put("list", list);

        return R.ok().data(map);
    }

    //添加课程基本信息的方法
    @ApiOperation(value = "添加课程基本信息的方法")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    //根据课程id查询课程基本信息
    @ApiOperation(value = "根据课程id查询课程基本信息接口")
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改课程信息
    @ApiOperation(value = "修改课程信息接口")
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @ApiOperation(value = "根据课程id查询课程确认信息接口")
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    //课程最终发布（修改课程状态）
    @ApiOperation(value = "课程最终发布接口（修改课程状态）")
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @ApiOperation(value = "根据课程id删除课程（课程本身、章节、小节、阿里云视频）接口")
    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

